//
//  UITextField+LZCategory.m
//  LZBaseFrame
//
//  Created by 位立志 on 2018/3/6.
//  Copyright © 2018年 Bwton. All rights reserved.
//

#import "UITextField+DGCategory.h"

@implementation UITextField (DGCategory)

/**
 限制输入长度
 
 @param textField textField
 @param length 允许输入的长度
 */
+ (void)textFieldDidChange:(UITextField *)textField andLength:(NSInteger)length{
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
    if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > length) {
                textField.text = [toBeString substringToIndex:length];
            }
        }
        else{//有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > length) {
            textField.text = [toBeString substringToIndex:length];
        }
    }
    
}


//TextField的代理方法  用来限制TextField允许输入的字符(对中文无效)
+ (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string andCanInputString:(NSString *)canInputString{
    
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:canInputString] invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    
    BOOL canChange = [string isEqualToString:filtered];
    
    return canChange;
}


//TextField的代理方法  用来限制TextField允许输入的字符(canInputString)以及长度(length) (对中文无效)
+ (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string andCanInputString:(NSString *)canInputString andLength:(NSInteger)length{
    
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:canInputString] invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    
    BOOL canChange = [string isEqualToString:filtered];
    
    BOOL canLength = YES;
    if (string.length == 0)
        canLength = YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (existedLength - selectedLength + replaceLength > length) {
        canLength = NO;
    }
    
    return canLength&&canChange;
}


//对中文无效
+ (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string andLength:(NSInteger)length{
    
    BOOL canLength = YES;
    if (string.length == 0)
        canLength = YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (existedLength - selectedLength + replaceLength > length) {
        canLength = NO;
    }
    
    return canLength;
}

@end
